# --------------------------------------------------------------------------------------------------------------------
# Project:             Digital Payment System (DPS)
# Website:             http://code.google.com/p/dps-x509/
# Purpose of document: All the generic functions that don't have a more
#                      appropriate home
# --------------------------------------------------------------------------------------------------------------------
import logging, exceptions

# --------------------------------------------------------------------------------------------------------------------
class LogOutputModes:
  """ defines the output formats for logging """
  NONE = 0  
  LOG_ONLY = 1
  PRINT_ONLY = 2
  LOG_AND_PRINT = 3
  
# --------------------------------------------------------------------------------------------------------------------
def setLogMode(mode):
  """ set the global log mode """
  global LOG_MODE
  assert(mode == LogOutputModes.NONE or \
         mode == LogOutputModes.LOG_ONLY or \
         mode == LogOutputModes.PRINT_ONLY or \
         mode == LogOutputModes.LOG_AND_PRINT)
  LOG_MODE = mode

# --------------------------------------------------------------------------------------------------------------------
def setLogLevel(level):
  """ level 0 -> always print. level 2 -> print only in debug """
  global LOG_LEVEL
  LOG_LEVEL = 0

# --------------------------------------------------------------------------------------------------------------------
def out(s, level = 0):
  """ print to standard out and log """
  global LOG_LEVEL
  global LOG_MODE
  
  if level <= LOG_LEVEL:
    if LOG_MODE == LogOutputModes.PRINT_ONLY or LOG_MODE == LogOutputModes.LOG_AND_PRINT:
      try:
        print(s)
      except:
        pass
    if LOG_MODE == LogOutputModes.LOG_ONLY or LOG_MODE == LogOutputModes.LOG_AND_PRINT:
      try:      
        logging.info(s)
      except:
        pass
      
# --------------------------------------------------------------------------------------------------------------------
def stringify(value, defaultIfNull=""):
  """ 
  attempt to convert string. returns defaultIfNull if null 
  @rtype: string 
  """
  if (value):
    return str(value)
  else:
    return defaultIfNull

# --------------------------------------------------------------------------------------------------------------------
def toInt(value):
  """ 
  attempt to convert string to int. returns 0 if it is not possible
  @rtype: int
  """
  v = 0
  try:
    v = int(value)
  except ValueError:
    pass
  
  return v

# --------------------------------------------------------------------------------------------------------------------
def assertion(test, message):
  """ if test is not true throw
  @param test: test to assert on
  @type  test: bool
  @param message: to display on error
  @type  message: string
  @raise exceptions.AssertionError: if test == False """
  isOk = test
  if not isOk:
    out("assertion failed: "+message)
    raise exceptions.AssertionError(message)


# --------------------------------------------------------------------------------------------------------------------
#set the filthy globals...
LOG_LEVEL = 0
LOG_MODE = LogOutputModes.LOG_AND_PRINT
